package com.jinke.api.modules.api.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jinke.api.common.api.CommonResult;
import com.jinke.api.modules.app.model.Order;
import com.jinke.api.modules.app.model.UserApi;
import com.jinke.api.modules.third.ebay.EbayService;
import com.jinke.api.modules.ums.util.UserUtil;
import com.jinke.api.modules.user.model.UserToken;
import com.jinke.api.modules.user.service.UserTokenService;
import com.jinke.api.security.util.ApiUserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/ebay")
@Slf4j
public class EbayContoller {

    @Resource
    private EbayService ebayService;

    @Resource
    private UserTokenService userTokenService;

    @PostMapping("/auth")
    public CommonResult<String> auth(@RequestBody StoreInfoRequest request) {
        String url = ebayService.getAuthorizaRestfulUrl(request);
        return CommonResult.success(url);
    }

    @PostMapping("/token")
    public CommonResult<String> token(@RequestBody StoreInfoRequest request) throws Exception {
        return ebayService.getTokenByCode(request);
    }

    //todo
    @GetMapping("refreshToken")
    public CommonResult refreshToken(Integer storeId) throws Exception {
        return ebayService.reRefreshToken(storeId);
    }

    @GetMapping("/listStore")
    public CommonResult<List<UserToken>> listStore() {
        LambdaQueryWrapper<UserToken> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserToken::getUserId, UserUtil.getUserInfo().getId());
        List<UserToken> list = userTokenService.list(wrapper);
        return CommonResult.success(list);
    }

    @GetMapping("/orders")
    public CommonResult<List<Order>> getOrders(Integer storeId) throws Exception {
        return ebayService.getOrders(storeId);
    }

    @GetMapping("/changeEnv")
    public CommonResult changeEnv() {
        return ebayService.changeEnv();
    }
}
